import { USER_LOGIN, INIT_USER } from './mutations-types.js';
export default {
    state: {


        //存储登录状态
        loginStatus: false,
        //token
        token: null,
        //用户信息(用户头像，用户昵称)
        userInfo: {}
    },
    getters: {},
    mutations: {
        // 设置
        [USER_LOGIN](state, user) {
            state.loginStatus = true; //登录状态为true
            state.token = user; //token
            state.userInfo = user; //用户信息
            // console.log(user);
            //将用户名存入localStorage
            localStorage.setItem("userInfo", JSON.stringify(user));
        },
        //读取
        [INIT_USER](state) {

            let userInfo = JSON.parse(localStorage.getItem("userInfo"));
            // console.log(userInfo);
            if(userInfo){
                state.loginStatus = true;
                state.token = userInfo.user;
                state.userInfo = userInfo;
            }
        },
        //退出登录
        loginOut(state){
            state.loginStatus = false;
            state.token = null;
            state.userInfo = {};
            localStorage.removeItem("userInfo");
        }
    },
    actions: {}
}